Completed
Push — master ( 7588c2...f404fc )
by Maxence
03:40
created

Circles.initialize   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 14
rs 9.4285
cc 1
nc 1
nop 3
1
/*
2
 * Circles - Bring cloud-users closer together.
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the COPYING file.
6
 *
7
 * @author Maxence Lange <[email protected]>
8
 * @copyright 2017
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
/** global: OC */
27
/** global: OCA */
28
29
(function () {
30
31
32
	/**
33
	 * @constructs Circles
34
	 */
35
	var Circles = function () {
36
		this.initialize();
37
	};
38
39
	Circles.prototype = {
40
41
42
		initialize: function () {
43
44
			var self = this;
45
46
47
			/**
48
			 * API function to create a new Circle.
49
			 *
50
			 * @param type
51
			 * @param name
52
			 * @param callback
53
			 */
54
			this.createCircle = function (type, name, callback) {
55
56
				var result = {status: -1};
57
				$.ajax({
58
					method: 'PUT',
59
					url: OC.generateUrl('/apps/circles/v1/circles'),
60
					data: {
61
						type: type,
62
						name: name
63
					}
64
				}).done(function (res) {
65
					self.onCallback(callback, res);
66
				}).fail(function () {
67
					self.onCallback(callback, result);
68
				});
69
			};
70
71
72
			this.listCircles = function (type, name, level, callback) {
73
				var result = {status: -1};
74
				$.ajax({
75
					method: 'GET',
76
					url: OC.generateUrl('/apps/circles/v1/circles'),
77
					data: {
78
						type: type,
79
						name: name,
80
						level: level
81
					}
82
				}).done(function (res) {
83
					self.onCallback(callback, res);
84
				}).fail(function () {
85
					self.onCallback(callback, result);
86
				});
87
			};
88
89
90
			this.detailsCircle = function (circleId, callback) {
91
				var result = {status: -1};
92
				$.ajax({
93
					method: 'GET',
94
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId)
95
				}).done(function (res) {
96
					self.onCallback(callback, res);
97
				}).fail(function () {
98
					self.onCallback(callback, result);
99
				});
100
			};
101
102
103
			this.addMember = function (circleId, userId, callback) {
104
				var result = {status: -1};
105
				$.ajax({
106
					method: 'PUT',
107
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/member'),
108
					data: {
109
						name: userId
110
					}
111
				}).done(function (res) {
112
					self.onCallback(callback, res);
113
				}).fail(function () {
114
					self.onCallback(callback, result);
115
				});
116
			};
117
118
119
			this.addEmail = function (circleId, email, callback) {
120
				var result = {status: -1};
121
				$.ajax({
122
					method: 'PUT',
123
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/email'),
124
					data: {
125
						email: email
126
					}
127
				}).done(function (res) {
128
					self.onCallback(callback, res);
129
				}).fail(function () {
130
					self.onCallback(callback, result);
131
				});
132
			};
133
134
135
			this.addGroupMembers = function (circleId, groupId, callback) {
136
				var result = {status: -1};
137
				$.ajax({
138
					method: 'PUT',
139
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/groupmembers'),
140
					data: {
141
						name: groupId
142
					}
143
				}).done(function (res) {
144
					self.onCallback(callback, res);
145
				}).fail(function () {
146
					self.onCallback(callback, result);
147
				});
148
			};
149
150
151
152
			this.removeMember = function (circleId, userId, callback) {
153
				var result = {status: -1};
154
				$.ajax({
155
					method: 'DELETE',
156
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/member'),
157
					data: {
158
						member: userId
159
					}
160
				}).done(function (res) {
161
					self.onCallback(callback, res);
162
				}).fail(function () {
163
					self.onCallback(callback, result);
164
				});
165
			};
166
167
168
			this.levelMember = function (circleId, member, level, callback) {
169
				var result = {status: -1};
170
				$.ajax({
171
					method: 'POST',
172
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/level'),
173
					data: {
174
						member: member,
175
						level: level
176
					}
177
				}).done(function (res) {
178
					self.onCallback(callback, res);
179
				}).fail(function () {
180
					self.onCallback(callback, result);
181
				});
182
			};
183
184
185
			this.linkGroup = function (circleId, groupId, callback) {
186
				var result = {status: -1};
187
				$.ajax({
188
					method: 'PUT',
189
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/groups'),
190
					data: {
191
						name: groupId
192
					}
193
				}).done(function (res) {
194
					self.onCallback(callback, res);
195
				}).fail(function () {
196
					self.onCallback(callback, result);
197
				});
198
			};
199
200
201
			this.unlinkGroup = function (circleId, groupId, callback) {
202
				var result = {status: -1};
203
				$.ajax({
204
					method: 'DELETE',
205
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/groups'),
206
					data: {
207
						group: groupId
208
					}
209
				}).done(function (res) {
210
					self.onCallback(callback, res);
211
				}).fail(function () {
212
					self.onCallback(callback, result);
213
				});
214
			};
215
216
217
			this.levelGroup = function (circleId, group, level, callback) {
218
				var result = {status: -1};
219
				$.ajax({
220
					method: 'POST',
221
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/group/level'),
222
					data: {
223
						group: group,
224
						level: level
225
					}
226
				}).done(function (res) {
227
					self.onCallback(callback, res);
228
				}).fail(function () {
229
					self.onCallback(callback, result);
230
				});
231
			};
232
233
234
			this.joinCircle = function (circleId, callback) {
235
				var result = {status: -1};
236
				$.ajax({
237
					method: 'GET',
238
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/join'),
239
					data: {}
240
				}).done(function (res) {
241
					self.onCallback(callback, res);
242
				}).fail(function () {
243
					self.onCallback(callback, result);
244
				});
245
			};
246
247
248
			this.settingsCircle = function (circleId, settings, callback) {
249
				var result = {status: -1};
250
				$.ajax({
251
					method: 'POST',
252
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/settings'),
253
					data: {settings: settings}
254
				}).done(function (res) {
255
					self.onCallback(callback, res);
256
				}).fail(function () {
257
					self.onCallback(callback, result);
258
				});
259
			};
260
261
262
			this.leaveCircle = function (circleId, callback) {
263
				var result = {status: -1};
264
				$.ajax({
265
					method: 'GET',
266
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/leave'),
267
					data: {}
268
				}).done(function (res) {
269
					self.onCallback(callback, res);
270
				}).fail(function () {
271
					self.onCallback(callback, result);
272
				});
273
			};
274
275
276
			this.destroyCircle = function (circleId, callback) {
277
				var result = {status: -1};
278
				$.ajax({
279
					method: 'DELETE',
280
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId),
281
					data: {}
282
				}).done(function (res) {
283
					self.onCallback(callback, res);
284
				}).fail(function () {
285
					self.onCallback(callback, result);
286
				});
287
			};
288
289
290
			this.shareToCircle = function (circleId, source, type, item, callback) {
291
				var result = {status: -1};
292
				$.ajax({
293
					method: 'PUT',
294
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/share'),
295
					data: {
296
						source: source,
297
						type: type,
298
						item: item
299
					}
300
				}).done(function (res) {
301
					self.onCallback(callback, res);
302
				}).fail(function () {
303
					self.onCallback(callback, result);
304
				});
305
			};
306
307
308
			this.linkCircle = function (circleId, remote, callback) {
309
				var result = {status: -1};
310
				$.ajax({
311
					method: 'POST',
312
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/link'),
313
					data: {
314
						remote: remote
315
					}
316
				}).done(function (res) {
317
					self.onCallback(callback, res);
318
				}).fail(function () {
319
					self.onCallback(callback, result);
320
				});
321
			};
322
323
324
			this.linkStatus = function (linkId, status, callback) {
325
				var result = {status: -1};
326
				$.ajax({
327
					method: 'POST',
328
					url: OC.generateUrl('/apps/circles/v1/link/' + linkId + '/status'),
329
					data: {
330
						status: status
331
					}
332
				}).done(function (res) {
333
					self.onCallback(callback, res);
334
				}).fail(function () {
335
					self.onCallback(callback, result);
336
				});
337
			};
338
339
			this.onCallback = function (callback, result) {
340
				if (callback && (typeof callback === 'function')) {
341
					if (typeof result === 'object') {
342
						callback(result);
343
					} else {
344
						callback({status: -1})
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
345
					}
346
				}
347
			};
348
349
		}
350
351
	};
352
353
	OCA.Circles = Circles;
354
	OCA.Circles.api = new Circles();
355
356
})();
357
358
359